package com.huaao.micropolice.root.controllers.alert;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huaao.micropolice.core.common.Context;
import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.model.police.Alert;
import com.huaao.micropolice.core.service.alert.AlertService;
import com.huaao.micropolice.root.controllers.BaseController;

@Controller("alertMSCtrl")
@RequestMapping("alert")
public class AlertController extends BaseController{


	@Autowired
	private AlertService alertService;
	@RequestMapping("index")
	public String index(){
		
		return "alert/alertIndex";
	}
	
	
	@RequestMapping("form")
	public String form(String alertId,HttpServletRequest reqeuest){
		Alert alert = new Alert();
		if (StringUtils.isNotBlank(alertId)){
			alert =  alertService.getBy(alertId);
		}
		
		request.setAttribute("alert", alert); 
		return "alert/alertForm";
	}
	/**
	 * @return 内容分页列表
	 */
	@RequestMapping("list")
	@ResponseBody
	public ResponseEntity<Map<String, Object> > list(){
		Page page = getPage();
		List<Alert> alertList = alertService.list(page);
		List<Map<String, Object>> respList = new ArrayList<>();
		for (Alert alert : alertList) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", alert.getId());
			map.put("title", alert.getTitle());
			map.put("type", alert.getType());
			map.put("description", alert.getDescription());
			map.put("address", alert.getAddress());
			map.put("imgs", alert.getImgs());
			map.put("voices", alert.getVoices());
			map.put("videos", alert.getVideos());
			respList.add(map);
		}
		
		Map<String, Object> body = successMap();
		body.put("data", respList);
		Long recordsCount = alertService.getCount();
		body.put("records", recordsCount);
		body.put("total",Math.floor((double)recordsCount/page.getPageSize())+1);
		
		return ajaxModel(MediaType.APPLICATION_JSON, body);
	}
	
	
	
	
	/**
	 * @param alert 新添加内容
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> save(Alert alert){
		if(StringUtils.isBlank(alert.getId())){
			alert.setId(null);
			alert.setCreateTime(new Date());
			alert.setCreateBy(Context.getCurrentUser());
			alert.setUpdateTime(new Date());
			alert.setUpdateBy(Context.getCurrentUser());
			
			String id = alertService.add(alert);
		}else{
			alert.setUpdateBy(Context.getCurrentUser());
			alert.setUpdateTime(new Date());
			
			alertService.update(alert);
		}
		
		alert = alertService.getBy(alert.getId());
				
		Map<String, Object> map = new HashMap<>();
		map.put("id", alert.getId());
		map.put("title", alert.getTitle());
		map.put("type", alert.getType());
		map.put("description", alert.getDescription());
		map.put("address", alert.getAddress());
		map.put("location", alert.getLocation());
		map.put("imgs", alert.getImgs());
		map.put("voices", alert.getVoices());
		map.put("videos", alert.getVideos());
		
		Map<String, Object> body = successMap();
		body.put("data", map);
		return ajaxModel(MediaType.APPLICATION_JSON, body);
		
	}
	
	
	@RequestMapping("delete")
	@ResponseBody
	public ResponseEntity<Map<String, Object>> delete(String alertId){
		Assert.hasText(alertId, "ID不能为空");
		alertService.delete(alertId);
		return ajaxModel(MediaType.APPLICATION_JSON, successMap());
	}
	
	


}
